When the same code is duplicated in two or more separate branches of a conditional, it can make the code harder to understand, maintain, and can
potentially introduce bugs if one instance of the code is changed but others are not.
Having two WHEN
in the same SELECT
statement or branches in the same IF
structure with the same
implementation is at best duplicate code, and at worst a coding error.
C IF X = 1
C EXSR SR01
C EXSR SR01
C ELSEIF X = 2
C EXSR SR02
C ELSEIF X = 3
C EXSR SR01 Noncompliant; duplicates first condition
C EXSR SR01
C ENDIF
select;
when i = 1;
doFirst();
doSomething();
when i = 2;
doSomethingDifferent();
when i = 3: // Noncompliant; duplicates first when's implementation
doFirst();
doSomething();
endsl;
if (a >= 0 and a < 10);
doFirst();
doTheThing();
elseif (a >= 10 and a < 20);
doTheOtherThing();
elseif (a >= 20 and a < 50);
doFirst(); // Noncompliant; duplicates first condition
doTheThing();
else;
doTheRest();
endif;
If the same logic is needed for both instances, they should be combined.